package com.adam.springmvc.controller;

import com.adam.springmvc.dao.StudentDAO;
import com.adam.springmvc.domain.Student;
import com.adam.springmvc.generator.model.Course;
import com.adam.springmvc.service.CourseService;
import com.adam.springmvc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class HelloController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;

    @RequestMapping("")
    public String index() {
        return "index";
    }

    @RequestMapping("findAllStudents")
    @ResponseBody
    public List<Student> findAllStudents() {
        return studentService.findAllStudents();
    }

    @RequestMapping("findStudentById")
    @ResponseBody
    public Student findStudentById(@RequestParam int id) {
        return studentService.findStudentById(id);
    }

    @RequestMapping("findCourseById")
    @ResponseBody
    public Course findCourseById(@RequestParam int id) {
        return courseService.findCourseById(id);
    }

    @RequestMapping("findCoursesByDate")
    @ResponseBody
    public List<Course> findCoursesByDate(@RequestParam String start, @RequestParam String end) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = simpleDateFormat.parse(start), endDate = simpleDateFormat.parse(end);
        return courseService.findCoursesByDate(startDate, endDate);
    }

}
